﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace lchealthy
{
    public partial class frmNewProduct : Form
    {
        public int pno = -1;
       //定义图片的文件路径
        string file = "";
        Image im;
        public frmNewProduct()
        {
            InitializeComponent();
        }
        public frmNewProduct(int id)
        {
            pno = id;
            InitializeComponent();
        }
        private void btnPic_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片|*.jpg;*.bmp;*.gif;*.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (pictureBox1.Image != null)
                {
                    im.Dispose();
                }
                file = openFileDialog1.FileName;
                im = Image.FromFile(file);
                pictureBox1.Image = im;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string pic = "";
            string no = txtNO.Text;
            if (pno == -1)//添加新产品
            {

                if (txtName.Text != "" && txtNO.Text != "")
                {

                    string sql1 = string.Format("select * from [ProductInfo] where [PName]='{0}'", txtName.Text);
                    DataSet ds = DbHelperOleDb.Query(sql1);
                    if (ds.Tables[0].Rows.Count >= 1)
                    {
                        MessageBox.Show("该产品已经存在，不能重复添加。", "提示");
                    }
                    else
                    {

                        if (file != "")
                        {
                            string extendedName = Path.GetExtension(file);
                            pic = "\\pimg\\" + no + extendedName;
                            if (File.Exists(Application.StartupPath + "\\pimg\\" + no + extendedName))
                                File.Delete(Application.StartupPath + "\\pimg\\" + no + extendedName);
                            File.Copy(file, Application.StartupPath + "\\pimg\\" + no + extendedName);
                        }
                        string sql = string.Format("INSERT INTO [ProductInfo] ([PName],[PNum],[PPrice],[PExplain],[PWeight],[PUsing],[PPic]) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", txtName.Text, no, txtPrice.Text, txtExplain.Text, txtWeight.Text, txtUsing.Text, pic);
                        int i = DbHelperOleDb.ExecuteSql(sql);
                        if (i >= 1)
                        {
                            MessageBox.Show("产品添加成功！");
                        }
                        else
                        {
                            MessageBox.Show("产品添加失败！");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请输入产品名称和编号！", "提示");
                }
            }
            else//修改已有产品的信息
            {
                string sql = "";

                if (file != "")
                {
                    string extendedName = Path.GetExtension(file);
                    pic = "\\pimg\\" + no + extendedName;
                    if (File.Exists(Application.StartupPath + "\\pimg\\" + no + extendedName))
                        File.Delete(Application.StartupPath + "\\pimg\\" + no + extendedName);
                    File.Copy(file, Application.StartupPath + "\\pimg\\" + no + extendedName);
                }
               
                if (file == "")
                    sql = string.Format("update ProductInfo set [PName] = '{0}',[PNum] ='{1}',[PPrice] = {2},[PExplain] = '{3}',[PWeight] = '{4}',[PUsing] = '{5}' where [PID]={6}", txtName.Text, txtNO.Text, txtPrice.Text, txtExplain.Text, txtWeight.Text, txtUsing.Text, pno);
                else
                    sql = string.Format("update ProductInfo set [PName] = '{0}',[PNum] ='{1}',[PPrice] = {2},[PPic] = '{3}',[PExplain] = '{4}',[PWeight] = '{5}',[PUsing] = '{6}' where [PID]={7}", txtName.Text, txtNO.Text, txtPrice.Text, pic, txtExplain.Text, txtWeight.Text, txtUsing.Text, pno);
                
                int i = DbHelperOleDb.ExecuteSql(sql);
                if (i == 1)
                {
                    MessageBox.Show("修改成功！");
                    this.Close();

                }
                else
                {
                    MessageBox.Show("修改失败！");
                }


            }
        }


        private void btnClear_Click(object sender, EventArgs e)
        {
            txtExplain.Text = null;
            txtName.Text = null;
            txtNO.Text = null;
            txtPrice.Text = null;
            txtUsing.Text = null;
            txtWeight.Text = null;
            pictureBox1.Image = null;
            txtName.Focus();

        }

        private void frmNewProduct_Load(object sender, EventArgs e)
        {
            if (pno != -1)
            {
                string sql = string.Format("select [PID],[PName],[PNum],[PPrice],[PPic],[PExplain],[PWeight],[PUsing] from ProductInfo where [PID]={0} ", pno);
                DataSet ds = DbHelperOleDb.Query(sql);
                if (ds.Tables[0].Rows.Count == 1)
                {
                    txtName.Text = ds.Tables[0].Rows[0]["PName"].ToString();
                    txtNO.Text = ds.Tables[0].Rows[0]["PNum"].ToString();
                    txtExplain.Text = ds.Tables[0].Rows[0]["PExplain"].ToString();
                    txtPrice.Text = ds.Tables[0].Rows[0]["PPrice"].ToString();
                    txtUsing.Text = ds.Tables[0].Rows[0]["PUsing"].ToString();
                    txtWeight.Text = ds.Tables[0].Rows[0]["PWeight"].ToString();
                    im = Image.FromFile(Application.StartupPath + ds.Tables[0].Rows[0]["PPic"].ToString());
                    pictureBox1.Image = im;
                }
            }
        }



    }
}
